﻿using System;
using System.Windows.Forms;
using SystemCustomers.DBUtils;
using SystemCustomers.ManageServices;

namespace SystemCustomers
{
    public partial class ManageServiceType : Form
    {
        public ManageServiceType()
        {
            InitializeComponent();
        }

        public ManageServiceType(string servicename)
            : this()
        {
            this.ServiceName = servicename;
        }

        private bool _canAdd = false;

        private void EnableAdd()
        {
            btnAdd.Enabled = btnUpdate.Enabled = IsValid() && _canAdd;
        }

        private bool IsValid()
        {
            return (txtbServiceName.Text.Trim() != string.Empty);
        }

        public string ServiceName
        {
            get { return txtbServiceName.Text; }
            set { txtbServiceName.Text = value; }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            MessageUtils.LogUtils.WriteToLog(" Exit Manage Service ");
            MessageUtils.LogUtils.SystemEventLogsInformation(" Exit Manage Service ");
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SingletonManagerDb.Instance.DbInsertService(ServiceName);
            MessageUtils.LogUtils.WriteToLog(" Add Service: " + ServiceName);
            MessageUtils.LogUtils.SystemEventLogsInformation(" Add Service:" + ServiceName);
            new MessageUtils.MessageBoxText(string.Format(" שרות נוסף בהצלחה: {0}", ServiceName)).OkMessaageBox();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            SingletonManagerDb.Instance.DbUpdateServices(FormViewingServices.Id, ServiceName);
            MessageUtils.LogUtils.WriteToLog(" Update Service: " + ServiceName);
            MessageUtils.LogUtils.SystemEventLogsInformation(" Update Service: " + ServiceName);
            new MessageUtils.MessageBoxText(string.Format(" שרות עודכן בהצלחה: {0}", ServiceName)).OkMessaageBox();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtbServiceName_TextChanged(object sender, EventArgs e)
        {
            if (!MyRegEx.Validate(txtbServiceName.Text, MyRegEx.OnlyCharsSpacesAndApostropheDigitsHebrew))
            {
                errorPServices.SetError(txtbServiceName, "רק אותיות גרש ורווחים מותרים");
                _canAdd = false;
            }
            else
            {
                errorPServices.SetError(txtbServiceName, string.Empty);
                _canAdd = true;
            }
            EnableAdd();
        }
    }
}
